//
//  TBRLoginController.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/4.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRLoginController.h"
#import "UIColor+Hex.h"
#import "TDInputPhoneNumC.h"
#import "BONoticeBar.h"
#import "NetWorkRequest.h"
#import <JKCountDownButton.h>

#import "NSString+MD5Addition.h"

@interface TBRLoginController ()

@property (weak, nonatomic) IBOutlet UIView *view_bg;

@property (weak, nonatomic) IBOutlet JKCountDownButton *btn_getYanZhengMa;

@property (weak, nonatomic) IBOutlet UITextField *tf_phone;
@property (weak, nonatomic) IBOutlet UITextField *tf_password;

@property (weak, nonatomic) IBOutlet UIButton *btn_login;
@property (weak, nonatomic) IBOutlet UIButton *btn_register;

@property (weak, nonatomic) IBOutlet UIButton *btn_forgetPW;

@property (nonatomic,copy)NSString *phoneNumStr;
@property (nonatomic,copy)NSString *passwordStr;

@end

@implementation TBRLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    self.view.backgroundColor = [UIColor whiteColor];
    _btn_login.layer.cornerRadius = 5;
    _btn_login.layer.masksToBounds = YES;
    _tf_password.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [_tf_password addTarget:self action:@selector(passwordStrChange:) forControlEvents:UIControlEventEditingChanged];
    _tf_phone.text = [AppSetting shareAppSetting].phone;
}

- (void)passwordStrChange:(UITextField *)sender {
    _passwordStr = sender.text;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tf_phone becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}

- (void)setupNavItem {
    [self ISG_NavigationBarDefaultBackButtonAndNavigationTitle:@"登录"];
    [self ISG_NavigationBarWithBackButtonTitle:nil leftImageNamed:nil andAction:@selector(popToRootViewController)];
}

- (IBAction)closeBtnClick:(UIButton *)sender {
    [self popToRootViewController];
}


- (void)popToRootViewController {
    // 清除用户信息------------------
    //    [[ISGUserInfo sharedUserInfo] logout];
    [self dismissViewControllerAnimated:YES completion:nil];
    //    [self.navigationController popToRootViewControllerAnimated:YES];
}




- (IBAction)loginBtnClick:(UIButton *)sender {
    [self.view endEditing:YES];
    NSString *account = self.tf_phone.text;
    NSString *passwordStr = _passwordStr;;
    
    if ([HelpObject isBlankString:account]) {
        self.noticeText = @"请先输入手机号码";
        return;
    }else if (account.length != 11) {
        self.noticeText = @"请输入11位手机号码";
        return;
    }
    
    
    
    //输入只能是字母和数字---------------判断账号输入的类型-------------
    NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"];
    s = [s invertedSet];
    NSRange r = [passwordStr rangeOfCharacterFromSet:s];
    if (passwordStr.length == 0) {
        self.noticeText = @"密码不能为空";
        return;
    }
    else if (r.location != NSNotFound) {
        self.noticeText = @"不能输入字母数字_以外的字符";
        return;
    }  else if (passwordStr.length < 6) {
        self.noticeText = @"密码不能小于6位";
        return;
    } else if (passwordStr.length > 20) {
        self.noticeText = @"密码不能大于20位";
        return;
    } else {
        NSDictionary *parDic = @{@"phone":account,@"password":[passwordStr MD5String],@"devicetoken":@"ios"};
        [self loginBtnDidClick:parDic];
    }
}

- (void)loginBtnDidClick:(NSDictionary *)dic {
    [self.view endEditing:YES];
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:@"登录中..."];
    // MARK: - 登录操作--------------
    [NetWorkRequest postNetRequestData:LoginTD parmeters:dic response:^(NSDictionary *response) {
        if (response) {
            //保存数据---进行一系列的操作----------
            [weakSelf progressHUDShowWithCompleteText:@"登录成功" isSucceed:YES];
            [AppSetting shareAppSetting].isLogin = @"1";
            
            [AppSetting shareAppSetting].memberid = response[@"data"][@"memberid"];
            [AppSetting shareAppSetting].tokenid = response[@"data"][@"tokenid"];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"登录失败，请重新登录" isSucceed:NO];
    }];
}



- (IBAction)registerBtnClick:(UIButton *)sender {
    [self.view endEditing:YES];
    TDInputPhoneNumC *vc = [TDInputPhoneNumC new];
    vc.isRegister = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)forgetPWBtnClick:(UIButton *)sender {
    [self.view endEditing:YES];
    TDInputPhoneNumC *vc = [TDInputPhoneNumC new];
    [self.navigationController pushViewController:vc animated:YES];
}



- (void)getYanZhengMaBtnClick {
    // MARK: - 登录操作--------------
    NSString *phone = self.tf_phone.text;
    NSDictionary *parDic = @{@"phone":phone};
    if ([HelpObject isBlankString:phone]) {
        self.noticeText = @"请先输入手机号码";
        return;
    }else if (phone.length != 11) {
        self.noticeText = @"请输入11位手机号码";
        return;
    }
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:@"获取验证码..."];
    [NetWorkRequest postNetRequestData:getYanZhengMa parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            weakSelf.btn_getYanZhengMa.enabled = NO;
            [weakSelf.btn_getYanZhengMa startCountDownWithSecond:60.0];
            [weakSelf.btn_getYanZhengMa countDownChanging:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                NSString *title = [NSString stringWithFormat:@"剩余%ld秒",second];
                return title;
            }];
            [weakSelf.btn_getYanZhengMa countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                countDownButton.enabled = YES;
                return @"获取验证码";
            }];
            [weakSelf progressHUDShowWithCompleteText:@"验证码获取成功" isSucceed:YES];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"验证码获取失败" isSucceed:NO];
    }];
}

@end
